from framework.interface.abstract_api import AbstractApi
from marshmallow import Schema, fields
import requests

# 请访问： http://127.0.0.1:5000/v1/eye-model
# 监测当前官网账号是否稳定运行？

#  获取中华香烟品牌
class EyeModel(AbstractApi):


    # 参数 模式
    class GetSchema(Schema):
        pass

    # 请求体
    def handle_get_request(self):
        result= self.eye_hearts()
        return result

    def eye_hearts(self):
    # 请求URL
        url = "http://106.53.97.117:3031/api/chat-process"

        # 请求头信息
        headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ja;q=0.5",
            "Connection": "keep-alive",
            # 根据需要添加其他头部信息
        }

        # 负载数据
        payload = {
            "prompt": "你是否正常？是就仅仅回答true",
            "options": {},
            "systemMessage": "You are ChatGPT",
            "temperature": 0.8,
            "top_p": 1
        }
        try:
            # 发送POST请求，并设置超时时间（例如：40秒）
            response = requests.post(url, json=payload, headers=headers, timeout=40)
            # 检查状态码是否为200，以确认请求成功
            if response.status_code == 200:
                # 输出响应内容
                print(response.text)
                data = response.json()
                if data["message"]=="[OpenAI] 服务器拒绝访问，请稍后再试 | Server refused to access, please try again later":
                    return False
                elif data["message"]=="[OpenAI] 服务器繁忙，请稍后再试 | Server is busy, please try again later":
                    return False
                else:
                    return True
            else:
                return False

        except requests.exceptions.Timeout:
            return False
        except requests.exceptions.RequestException as e:
            print("监测请求异常 切换配置文件")
            # 处理其他可能发生的异常
            print("请求出现问题：", e)
            return False
